﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using Maticsoft.DBUtility;
using System.Text;

namespace SkWeb.RiskDB
{
    /// <summary>
    /// Handler1 的摘要说明
    /// </summary>
    public class Handler1 : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            try
            {
                string resultJson = string.Empty;

                string strSql = "select id ,name,lng,lat, jb,gy,rank  from tb_wellinfo t where rownum<80";
                DataTable dt = OracleHelper.Query(strSql).Tables[0];
                string strCount = dt.Rows.Count.ToString();
                StringBuilder resultBuilder = new StringBuilder("{\"total\":" + strCount + ",\"rows\":[");
                string jsonFormat = "{{\"id\":\" {0}\",\"name\": \"{1}\", \"lng\": \"{2}\",\"lat\": \"{3}\", \"jb\": \"{4}\",\"gy\": \"{5}\",\"rank\": {6}}},";
                if (dt != null && dt.Rows.Count > 0)
                {
                  
                    foreach (var row in dt.AsEnumerable())
                    {
                        string strId = Convert.IsDBNull(row["id"]) ? "" : Convert.ToString(row["id"]);
                        string strJh = Convert.IsDBNull(row["name"]) ? "" : Convert.ToString(row["name"]);
                        string strLng = Convert.IsDBNull(row["lng"]) ? "" : Convert.ToString(row["lng"]);
                        string strLat = Convert.IsDBNull(row["lat"]) ? "" : Convert.ToString(row["lat"]);
                        string strJb = Convert.IsDBNull(row["jb"]) ? "" : Convert.ToString(row["jb"]);
                        string strGy = Convert.IsDBNull(row["gy"]) ? "" : Convert.ToString(row["gy"]);
                        string strRank = Convert.IsDBNull(row["rank"]) ? "0" : (Convert.ToInt32(row["rank"]) % 10).ToString();

                        resultBuilder.AppendFormat(jsonFormat, strId, strJh,
                             strLng, strLat, strJb, strGy, strRank);
                    }
                }



                if (resultBuilder.Length > 1)
                {
                    resultBuilder = resultBuilder.Remove(resultBuilder.Length - 1, 1);
                }

                resultBuilder.Append("]}");
                resultJson = resultBuilder.ToString();

                context.Response.ContentType = "text/json";

                
                context.Response.Write(resultJson);
            }
            catch (Exception ex)
            {
                string s = ex.Message;
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}